/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataBasePanel.java
 *
 * Created on Jul 12, 2012, 10:52:03 AM
 */

package com.piza.toolcenter.view.mybatis;

import com.piza.toolcenter.model.DBInfo;
import com.piza.toolcenter.model.GeneratorConfig;
import com.piza.toolcenter.operation.IWorkFlow;
import com.piza.toolcenter.operation.OperationTree;
import com.piza.toolcenter.operation.SessionContext;
import java.io.File;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.plaf.basic.BasicComboBoxUI.ListDataHandler;

/**
 *
 * @author cwb
 */
public class DataBasePanel extends javax.swing.JPanel implements IWorkFlow{

    private DefaultComboBoxModel defultModel;
    private DefaultComboBoxModel configModel;
    
    private SessionContext context;
    /** Creates new form DataBasePanel */
    public DataBasePanel() {
        initComponents();
        this.dbIdTextField.setText("");
        this.connectURLTextField.setText("jdbc:mysql://localhost/robot");
        this.usernameTextField.setText("root");
        this.driverTextField.setText("com.mysql.jdbc.Driver");
        this.passwordField.setText("");
        
    }

    public void open(SessionContext context) {
        initDBList();
        initConfigList();
        this.context=context;
        this.listDBChanged();
        listConfigChanged();
    }

    public boolean close() {
        
        boolean ok=false;
        if(this.dBInfo1.getId()==null){
                    JOptionPane warnPane=new JOptionPane();
                    warnPane.showMessageDialog(this, "Invalid DBInfo! pls save first", "Warning", JOptionPane.DEFAULT_OPTION);
                  
                }else{
                     this.context.addItem("CurrentDBInfo", this.dBInfo1);
                    ok=true;
                }
        
        if(!this.checkProInput()){
            JOptionPane warnPane=new JOptionPane();
            warnPane.showMessageDialog(this, "generator check failed! pls save first", "Warning", JOptionPane.DEFAULT_OPTION);
                  
            ok=false;
        }else{
            this.context.addItem("genConfig", this.generatorConfig);
            ok=true;
        }
        
        return ok;
        
    }
    
    private void initConfigList(){
        try{
            List<GeneratorConfig> allConfig=OperationTree.getAllConfigInfo();
            if(allConfig==null){
                return;
            }
            configModel=new DefaultComboBoxModel(allConfig.toArray());
            this.configComboBox.setModel(configModel);
            configModel.addListDataListener(new ListDataListener() {

                public void intervalAdded(ListDataEvent e) {
                }

                public void intervalRemoved(ListDataEvent e) {
                }

                public void contentsChanged(ListDataEvent e) {
                   listConfigChanged();
                }
            });

        }catch(Exception e){
            warn("error when retrieve all config info");
        }
    }

    
    private void initDBList(){
        try{

        List<DBInfo> allDB= OperationTree.getAllDBInfo();
        if(allDB==null){
            return;
        }
         defultModel=new DefaultComboBoxModel(allDB.toArray());
        this.allDBComboBox.setModel(defultModel);
        defultModel.addListDataListener(new ListDataListener() {

                public void intervalAdded(ListDataEvent e) {

                }

                public void intervalRemoved(ListDataEvent e) {

                }

                public void contentsChanged(ListDataEvent e) {
                   listDBChanged();
                }
            });
        }catch(Exception e){
            warn("Error when retrieve all db");
        }
    }

    private void listConfigChanged(){
        Object obj=this.configModel.getSelectedItem();
        if(obj!=null){
            showSelectedConfigInfo((GeneratorConfig)obj);
        }
    }
    private void listDBChanged(){
        Object obj=defultModel.getSelectedItem();
        if(obj!=null){
                showSelectedInfo((DBInfo)obj);
        }
    }

    private void showSelectedConfigInfo(GeneratorConfig config){
        this.generatorConfig.setId(config.getId());
        this.locationTextField.setText(config.getConnectorLocation());
        this.enableSPCheckBox.setSelected(config.getEnableSubPackages());
        this.tarPackageTextField.setText(config.getTargetPackage());
        this.tarProjectTextField.setText(config.getTargetProject());
    }
    private void showSelectedInfo(DBInfo dbinfo){
        this.dBInfo1.setId(dbinfo.getId());
        this.dbIdTextField.setText(dbinfo.getDbId());
        this.connectURLTextField.setText(dbinfo.getConnectUrl());
        this.usernameTextField.setText(dbinfo.getUserName());
        this.driverTextField.setText(dbinfo.getDriverClass());
        this.passwordField.setText(dbinfo.getPassword());
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        dBInfo1 = new com.piza.toolcenter.model.DBInfo();
        generatorConfig = new com.piza.toolcenter.model.GeneratorConfig();
        jLabel1 = new javax.swing.JLabel();
        dbIdTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        driverTextField = new javax.swing.JTextField();
        connectURLTextField = new javax.swing.JTextField();
        usernameTextField = new javax.swing.JTextField();
        testButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        allDBComboBox = new javax.swing.JComboBox();
        saveButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        passwordField = new javax.swing.JPasswordField();
        jSeparator1 = new javax.swing.JSeparator();
        warnLabel = new javax.swing.JLabel();
        locationTextField = new javax.swing.JTextField();
        chooseLocationButton = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        tarProjectTextField = new javax.swing.JTextField();
        tarPackageTextField = new javax.swing.JTextField();
        enableSPCheckBox = new javax.swing.JCheckBox();
        chooseProjectButton = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        configComboBox = new javax.swing.JComboBox();
        saveProButton = new javax.swing.JButton();
        resetProButton = new javax.swing.JButton();

        jLabel1.setText("DB_Id");
        jLabel1.setName("jLabel1"); // NOI18N

        dbIdTextField.setName("dbIdTextField"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, dBInfo1, org.jdesktop.beansbinding.ELProperty.create("${dbId}"), dbIdTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setText("Driver Class");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Connect URL");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("User Name");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Password");
        jLabel5.setName("jLabel5"); // NOI18N

        driverTextField.setName("driverTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, dBInfo1, org.jdesktop.beansbinding.ELProperty.create("${driverClass}"), driverTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        connectURLTextField.setName("connectURLTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, dBInfo1, org.jdesktop.beansbinding.ELProperty.create("${connectUrl}"), connectURLTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        usernameTextField.setName("usernameTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, dBInfo1, org.jdesktop.beansbinding.ELProperty.create("${userName}"), usernameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        testButton.setText("Test");
        testButton.setName("testButton"); // NOI18N
        testButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testButtonActionPerformed(evt);
            }
        });

        jLabel6.setText("Select DB");
        jLabel6.setName("jLabel6"); // NOI18N

        allDBComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        allDBComboBox.setName("allDBComboBox"); // NOI18N

        saveButton.setText("Save");
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        resetButton.setText("reset");
        resetButton.setName("resetButton"); // NOI18N
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });

        passwordField.setName("passwordField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, dBInfo1, org.jdesktop.beansbinding.ELProperty.create("${password}"), passwordField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jSeparator1.setForeground(new java.awt.Color(51, 51, 51));
        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setName("jSeparator1"); // NOI18N

        warnLabel.setText("every input is can not be null");
        warnLabel.setName("warnLabel"); // NOI18N

        locationTextField.setName("locationTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, generatorConfig, org.jdesktop.beansbinding.ELProperty.create("${connectorLocation}"), locationTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        chooseLocationButton.setText("Connector DIR");
        chooseLocationButton.setName("chooseLocationButton"); // NOI18N
        chooseLocationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseLocationButtonActionPerformed(evt);
            }
        });

        jLabel9.setText("Target Package:");
        jLabel9.setName("jLabel9"); // NOI18N

        tarProjectTextField.setName("tarProjectTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, generatorConfig, org.jdesktop.beansbinding.ELProperty.create("${targetProject}"), tarProjectTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        tarPackageTextField.setName("tarPackageTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, generatorConfig, org.jdesktop.beansbinding.ELProperty.create("${targetPackage}"), tarPackageTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        enableSPCheckBox.setText("EnableSubpackage");
        enableSPCheckBox.setName("enableSPCheckBox"); // NOI18N
        enableSPCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enableSPCheckBoxActionPerformed(evt);
            }
        });

        chooseProjectButton.setText("Target Project");
        chooseProjectButton.setName("chooseProjectButton"); // NOI18N
        chooseProjectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseProjectButtonActionPerformed(evt);
            }
        });

        jLabel7.setText("Select Project");
        jLabel7.setName("jLabel7"); // NOI18N

        configComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        configComboBox.setName("configComboBox"); // NOI18N

        saveProButton.setText("Save");
        saveProButton.setName("saveProButton"); // NOI18N
        saveProButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveProButtonActionPerformed(evt);
            }
        });

        resetProButton.setText("reset");
        resetProButton.setName("resetProButton"); // NOI18N
        resetProButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetProButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(71, 71, 71)
                        .addComponent(jLabel6)
                        .addGap(41, 41, 41)
                        .addComponent(allDBComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(passwordField)
                            .addComponent(usernameTextField)
                            .addComponent(connectURLTextField)
                            .addComponent(driverTextField)
                            .addComponent(dbIdTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(testButton)
                        .addGap(43, 43, 43)
                        .addComponent(saveButton)
                        .addGap(41, 41, 41)
                        .addComponent(resetButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(chooseProjectButton)
                            .addComponent(chooseLocationButton)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(locationTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                            .addComponent(tarPackageTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                            .addComponent(tarProjectTextField)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(enableSPCheckBox))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel7)
                        .addGap(74, 74, 74)
                        .addComponent(configComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(saveProButton)
                        .addGap(47, 47, 47)
                        .addComponent(resetProButton)))
                .addGap(72, 72, 72))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(230, 230, 230)
                .addComponent(warnLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addGap(219, 219, 219))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4, jLabel5});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {chooseLocationButton, chooseProjectButton});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {locationTextField, tarPackageTextField, tarProjectTextField});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(configComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(chooseLocationButton)
                                .addGap(18, 18, 18)
                                .addComponent(chooseProjectButton)
                                .addGap(12, 12, 12)
                                .addComponent(jLabel9))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(locationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(tarProjectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)
                                .addComponent(tarPackageTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(53, 53, 53)
                        .addComponent(enableSPCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(saveProButton)
                            .addComponent(resetProButton)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(allDBComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(dbIdTextField))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(driverTextField))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(connectURLTextField))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(usernameTextField))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(resetButton)
                            .addComponent(testButton)
                            .addComponent(saveButton))))
                .addGap(18, 18, 18)
                .addComponent(warnLabel)
                .addGap(94, 94, 94))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
        if(checkInput()){
            warn(OperationTree.testJDBC(dBInfo1));
        }
    }//GEN-LAST:event_testButtonActionPerformed

    private boolean checkInput(){
        boolean ok=false;
        if(checkNull(this.dBInfo1.getDbId())){
          warn("DB id can not be null");
          return ok;
        }
         if(checkNull(this.dBInfo1.getDriverClass())){
          warn("Driver Class can not be null");
          return ok;
        }
         if(checkNull(this.dBInfo1.getConnectUrl())){
          warn("Connect URL can not be null");
          return ok;
        }
         if(checkNull(this.dBInfo1.getUserName())){
          warn("User Name can not be null");
          return ok;
        }
         if(checkNull(this.dBInfo1.getPassword())){
          warn("Password can not be null");
          return ok;
        }
        ok=true;
        return ok;
    }

    private void warn(String s){
        this.warnLabel.setText("<html><head></head><body><font color='red'>"+s+"</font></body></html>");
    }
    private boolean checkNull(String s){
        return (s==null || s.equals(""));
    }

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        this.dBInfo1.setId(null);
        this.dbIdTextField.setText("");
        this.connectURLTextField.setText("jdbc:mysql://localhost/robot");
        this.usernameTextField.setText("root");
        this.driverTextField.setText("com.mysql.jdbc.Driver");
        this.passwordField.setText("");
    }//GEN-LAST:event_resetButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
       if(!this.checkInput()){
           return;
       }
        boolean ok=OperationTree.saveJDBCConfig(dBInfo1);
       if(ok){
           this.initDBList();
           warn("Saved successful!");
       }else{
           warn("Saved failed!");
       }

    }//GEN-LAST:event_saveButtonActionPerformed

    
    private boolean checkProInput(){
        boolean ok=false;
        if(checkNull(this.generatorConfig.getConnectorLocation())){
          warn("Connector Location can not be null");
          return ok;
        }
         if(checkNull(this.generatorConfig.getTargetProject())){
          warn("Target Project can not be null");
          return ok;
        }
         if(checkNull(this.generatorConfig.getTargetPackage())){
          warn("Target Package can not be null");
          return ok;
        }
         
        ok=true;
        return ok;
    }
    
    private void chooseLocationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseLocationButtonActionPerformed
        
        
        JFileChooser chooser=new JFileChooser();
        chooser.setDialogTitle("Choose connector jar path!");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.showOpenDialog(null);
        File f=chooser.getSelectedFile();
        if(null!=f && f.exists()){
            this.locationTextField.setText(f.getAbsolutePath());
        }
    }//GEN-LAST:event_chooseLocationButtonActionPerformed

    private void chooseProjectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseProjectButtonActionPerformed
        JFileChooser chooser=new JFileChooser();
        chooser.setDialogTitle("Choose target project path!");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.showOpenDialog(null);
        File f=chooser.getSelectedFile();
        if(null!=f && f.exists()){
            this.tarProjectTextField.setText(f.getAbsolutePath());
        }
}//GEN-LAST:event_chooseProjectButtonActionPerformed

    private void saveProButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveProButtonActionPerformed

        if(!this.checkProInput()){
            return;
        }

        boolean ok=OperationTree.saveGenConfig(this.generatorConfig);
        if(ok){
            this.initConfigList();
            warn("Save config successful");
        }else{
            warn("Save failed!");
        }
    }//GEN-LAST:event_saveProButtonActionPerformed

    private void resetProButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetProButtonActionPerformed
        this.tarPackageTextField.setText("");
        this.tarProjectTextField.setText("");
        this.locationTextField.setText("");
        this.enableSPCheckBox.setSelected(false);
    }//GEN-LAST:event_resetProButtonActionPerformed

    private void enableSPCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableSPCheckBoxActionPerformed
       if(this.enableSPCheckBox.isSelected()){
           this.generatorConfig.setEnableSubPackages(true);
       }else{
           this.generatorConfig.setEnableSubPackages(false);
       }
    }//GEN-LAST:event_enableSPCheckBoxActionPerformed


    public DBInfo getCurrentDBInfo(){
        return this.dBInfo1;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox allDBComboBox;
    private javax.swing.JButton chooseLocationButton;
    private javax.swing.JButton chooseProjectButton;
    private javax.swing.JComboBox configComboBox;
    private javax.swing.JTextField connectURLTextField;
    private com.piza.toolcenter.model.DBInfo dBInfo1;
    private javax.swing.JTextField dbIdTextField;
    private javax.swing.JTextField driverTextField;
    private javax.swing.JCheckBox enableSPCheckBox;
    private com.piza.toolcenter.model.GeneratorConfig generatorConfig;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField locationTextField;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton resetProButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton saveProButton;
    private javax.swing.JTextField tarPackageTextField;
    private javax.swing.JTextField tarProjectTextField;
    private javax.swing.JButton testButton;
    private javax.swing.JTextField usernameTextField;
    private javax.swing.JLabel warnLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
